package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.document.DocumentModelUtil;
import m.system.RuntimeData;
import m.system.db.DataSet;
import m.system.db.SqlBuffer;
import m.system.document.DocumentUtil;
import m.system.util.JSONMessage;

@ActionMeta(name="manage/developer")
public class DeveloperAction extends ManageAction {
	
	public VueResult toIndex() throws Exception{
		verifyMenu("A1109");
		verifyAdminOperPower("manage_system_power");
        return new VuePageResult("page/base/developer/index.js")
			.addParam("models", DocumentModelUtil.documentList())
			.addParam("actions", DocumentUtil.documentList());
	}
	@RequestMeta(params = {"tableName"})
	public JSONMessage getTableField(String tableName) throws Exception {
		verifyMenu("A1109");
		verifyAdminOperPower("manage_system_power");
		String[] tn=tableName.split("\\.");
		SqlBuffer sql=new SqlBuffer()
			.append("SELECT column_name name,data_type type,character_maximum_length length,")
			.append("case when is_nullable='NO' then 'true' else null end notnull,")
			.append("column_default defaultValue,column_comment description, ")
			.append("null dictType,null dictOption,null dbType")
			.append("FROM information_schema.columns");
		if(tn.length>1){
			sql.append("WHERE table_schema = ? and table_name = ?",tn[0],tn[1]);
		}else{
			sql.append("WHERE table_name = ? and table_schema = ?",tn[0], RuntimeData.getTableSchema());
		}
		DataSet ds=sql.executeQuery();
		return new JSONMessage(0)
			.push("columns",ds);
	}
}
